package net.jueb.frameworkDemo.orm.mongodb.springDataMongodb.xmlStyle;

import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.mongodb.WriteResult;

import net.jueb.frameworkDemo.orm.mongodb.springDataMongodb.MyRepository;
import net.jueb.frameworkDemo.orm.mongodb.springDataMongodb.Person;

@Component
public class Center_Person_Dao implements MyRepository<Person> {
	
	@Resource
	MongoTemplate mongoTemplate_center;

	@Override
	public List<Person> getAllObjects() {
		return mongoTemplate_center.findAll(Person.class);
	}

	@Override
	public void saveObject(Person tree) {
		mongoTemplate_center.insert(tree);
	}
	
	@Override
	public void saveObjects(Collection<Person> trees) {
		mongoTemplate_center.insert(trees);
	}

	@Override
	public Person getObject(String id) {
		return mongoTemplate_center.findOne(new Query(Criteria.where("id").is(id)),
				Person.class);
	}

	@Override
	public WriteResult updateObject(String id, String name) {
		return mongoTemplate_center.updateFirst(
				new Query(Criteria.where("id").is(id)),
				Update.update("name", name), Person.class);
	}

	@Override
	public void deleteObject(String id) {
		mongoTemplate_center.remove(new Query(Criteria.where("id").is(id)),
				Person.class);
	}

	@Override
	public void createCollection() {
		if (!mongoTemplate_center.collectionExists(Person.class)) {
			mongoTemplate_center.createCollection(Person.class);
		}
	}

	@Override
	public void dropCollection() {
		if (mongoTemplate_center.collectionExists(Person.class)) {
			mongoTemplate_center.dropCollection(Person.class);
		}
	}
	
	@Override
	public Person findObject(String id) {
		Query query=new Query(Criteria.where("id").is(id));
		return mongoTemplate_center.findOne(query, Person.class);
	}
}